<template>
  <div v-if="browserRedirect">
    <div>
      <div class="table-page-search-wrapper" v-show="showSizer">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-item label="账号">
                <a-input placeholder="请输入账号" v-model="userName"></a-input>
              </a-form-item>
            </a-col>
            <template>
              <a-col :md="6" :sm="8">
                <a-form-item label="手机号码">
                  <a-input placeholder="请输入手机号码" v-model="phone"></a-input>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="8" >
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              </span>
            </a-col>
  
          </a-row>
        </a-form>
      </div>
      <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
        <div class="extra-wrapper" slot="tabBarExtraContent">
          <div class="selSty" @click="showSizer=!showSizer" style="line-height: 60px;">
            <span>筛选</span>
            <img style='margin-top: -3px;margin-left: 3px;' src="../../assets/common-icon-filter.svg">
          </div>
        </div>
        <a-tab-pane loading="true" tab="子账户管理" key="1" style="min-height: 100vh;">
          <ul style="margin: 0;padding:0;list-style-type: none;">
            <li v-for="val in dataSource" class="liCommSty">
              <div style="border-bottom: 1px solid #EBEBEB;padding-bottom: 10px;">
                <div style="color: #333;font-weight: bold;">
                  <span>{{val.userName}}</span>
                </div>
              </div>
              <div style="margin-top: 10px;">
                <span style="color: #333;">手机号码</span>
                <span style="float: right;">{{val.phone}}</span>
              </div>
              <div style="margin-top: 10px;">
                <span style="color: #333;">创建时间</span>
                <span style="float: right;">{{val.createTime}}</span>
              </div>
            </li>
          </ul>
          <a-pagination style="float: right;margin-top:5px;" showQuickJumper :defaultCurrent="ipagination.current" :total="ipagination.total" @change="onChange"/>
        </a-tab-pane>
      </a-tabs>
    </div>
  </div>
  <a-card v-else :bordered="false" :class="'cust-erp-sub-tab'" style="border-radius: 10px;">
    <!-- 操作按钮区域 -->
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="账号">
              <a-input placeholder="请输入账号" v-model="userName"></a-input>
            </a-form-item>
          </a-col>
          <template>
            <a-col :md="6" :sm="8">
              <a-form-item label="手机号码">
                <a-input placeholder="请输入手机号码" v-model="phone"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <div class="table-operator" v-if="userAuth2btn == 1 && listData.status == 1">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <blockManufacturerUser-modal ref="modalForm" @ok="modalFormOk" :mainId="mainId"></blockManufacturerUser-modal>
  </a-card>
</template>

<script>
  import { browserRedirect,filterObj } from '@/utils/util'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BlockManufacturerUserModal from './modules/BlockManufacturerUserModal'
  import { deleteAction,httpAction,getAction } from '@/api/manage'

  export default {
    name: "BlockManufacturerUserList",
    mixins:[JeecgListMixin],
    components: { BlockManufacturerUserModal },
    props:{
      showAdd:{
        type:Boolean,
        default:false
      },
      mainId:{
        type:String,
        default:'',
        required:false
      }
    },
    watch:{
      mainId:{
        immediate: true,
        handler(val) {
          if(!this.mainId){
            this.clearList()
          }else{
            this.queryParam['manufacturerId'] = val
            this.loadData(1);
          }
        }
      }
    },
    data () {
      return {
        browserRedirect:false,
        showSizer:false,
        listData:{},
        description: '发行方信息管理页面',
        disableMixinCreated:true,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
           {
            title:'账号',
            align:"center",
            dataIndex: 'userName'
          },
          {
            title:'创建日期',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'手机号码',
            align:"center",
            dataIndex: 'phone'
          },                    
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
        userName:"",
        phone:"",
        url: {
          list: "/block/blockManufacturerInfo/listBlockManufacturerUserByMainId",
          delete: "/block/blockManufacturerInfo/deleteBlockManufacturerUser",
          deleteBatch: "/block/blockManufacturerInfo/deleteBatchBlockManufacturerUser"
        },
        dictOptions:{
         status:[],
        },
        userAuth2btn:""
      }
    },
    mounted(){
      var that = this;
      that.browserRedirect = browserRedirect();
      getAction("/block/blockManufacturerInfo/userAuth2btn")
        .then(res => {
          if (res.success) {
            that.userAuth2btn = res.result.status;
            that.$forceUpdate();
          } else {
            that.$message.warning(res.message)
          }
        }) 
      let list1={
        column: "createTime",
        order: "desc",
        field: "id,,factoryName,companyProfile,createTime,type,name,status,address,phone,imageurl,action",
        pageNo: 1,
        pageSize: 5
      };
      getAction("/block/blockManufacturerInfo/list",list1).then((res)=>{
        if (res.success) {
          that.listData = res.result.records[0];
          if(!that.queryParam || !that.queryParam['manufacturerId']){
            that.queryParam={};
            that.queryParam['manufacturerId']="";
            that.queryParam['manufacturerId'] = that.listData.id;
          }
          that.loadData();
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    methods: {
      onChange(pageNumber) {
        this.ipagination.current = pageNumber;
        this.handleTableChange(this.ipagination,{},{});
      },
      searchReset(){
        this.userName="";
        this.phone="";
        this.loadData();
      },
      searchQuery(){
        if(this.userName){
          var valUser="*"+this.userName+"*"
        }else{
          var valUser="";
        }
        if(this.phone){
          var valPhone="*"+this.phone+"*"
        }else{
          var valPhone="";
        }
        let list2={
          userName:valUser,
          phone:valPhone,
          manufacturerId:this.queryParam['manufacturerId'],
          column: "createTime",
          order: "desc",
          field: "id,,,userName,createTime,phone,action",
          pageNo: 1,
          pageSize: 10
        };
        getAction("/block/blockManufacturerInfo/listBlockManufacturerUserByMainId",list2).then((res)=>{
          if (res.success) {
            this.dataSource = res.result.records;
            this.$forceUpdate();
          } else {
            this.$message.warning(res.message)
          }
        })
      },
      batchDel: function () {
        if(!this.url.deleteBatch){
          this.$message.error("请设置url.deleteBatch属性!")
          return
        }
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            ids += this.selectedRowKeys[a] + ",";
          }
          var that = this;
          this.$confirm({
            title: "确认删除",
            content: "是否删除选中数据?",
            onOk: function () {
              that.loading = true;
              deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              })
            }
          });
        }
      },
      loadData(){
        let list2={
          manufacturerId:this.queryParam['manufacturerId'],
          column: "createTime",
          order: "desc",
          field: "id,,,userName,createTime,phone,action",
          pageNo: 1,
          pageSize: 10
        };
        getAction("/block/blockManufacturerInfo/listBlockManufacturerUserByMainId",list2).then((res)=>{
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
            this.$forceUpdate();
          } else {
            this.$message.warning(res.message)
          }
        })
      },
      handleDelete(id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      clearList(){
        this.dataSource=[]
        this.selectedRowKeys=[]
        this.ipagination.current = 1
      },
      modalFormOk(){
        this.loadData();
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>
